#include "EnemyWhite.h"



CEnemy_White::CEnemy_White()
{
	mark = 0;
	count_enemy = 0;
	frame_start_white = GetTickCount();
	tick_per_frame = 1000 / FRAME_RATE;
}

void CEnemy_White::LoadResource(LPDIRECT3DDEVICE9 d3ddv, LPD3DXSPRITE &SpriteHandler, HINSTANCE hInstance, HWND hWnd)
{
	for (int i = 0; i < MAX_ENEMY_WHITE; i++)
	{
		_EnemyWhite[i] = new CSprite(SpriteHandler, L"resources\\enemy_left.png", 248 / 6, 66, 6, 6, D3DCOLOR_XRGB(255, 0, 255));
	}

	_EnemyWhitePosition.x = -100;
	_EnemyWhitePosition.y = GROUNDY_ENEMY;

	for (int i = 0; i < MAX_ENEMY_WHITE; i++)
	{
		_EnemyVector[i].x = SPEED_ENEMY;
		_EnemyVector[i].y = 0;
	}

}

void CEnemy_White::Update(D3DXVECTOR2 ContraPositon)
{
	DWORD now = GetTickCount();

	//Enemy White

	//position to delete enemy
	if (ContraPositon.x >= MARK2 && ContraPositon.x < MARK2 + 20)
		mark = 2;
	if (ContraPositon.x >= MARK3 && ContraPositon.x < MARK3 + 20)
		mark = 3;
	if (ContraPositon.x >= MARK4 && ContraPositon.x < MARK4 + 20)
		mark = 4;
	if (ContraPositon.x >= MARK5 && ContraPositon.x < MARK5 + 20)
		mark = 5;

	if ((ContraPositon.x  <  MARK1  && ContraPositon.x >100))
	{
		_EnemyWhitePosition.y = GROUNDY_ENEMY;

		if (count_enemy == 0 && ContraPositon.x > 360 && mark == 0)
		{
			_EnemyWhitePosition.x = 3 * SCREEN_WIDTH / 2;
			mark++;
		}

		if (count_enemy < MAX_ENEMY_WHITE - 1 && ContraPositon.x > 360 && _EnemyWhitePosition.x <= ContraPositon.x - SCREEN_WIDTH / 2)
		{
			count_enemy++;
			_EnemyWhitePosition.x = SCREEN_WIDTH + _ContraPosition.x;
		}
		if (now - frame_start_white >= tick_per_frame)
		{
			frame_start_white = now;
			_EnemyWhite[count_enemy]->Next();
		}
	}
	else if ((ContraPositon.x >MARK1 && ContraPositon.x < MARK2 && mark == 1))
	{
		_EnemyWhitePosition.y = 180;

		if (count_enemy < MAX_ENEMY_WHITE - 1  && _EnemyWhitePosition.x <= ContraPositon.x - SCREEN_WIDTH / 2)
		{
			count_enemy += 2;
			_EnemyWhitePosition.x = 1728;
		}

		if (_EnemyWhitePosition.x < 1550)
		{

			if (_EnemyWhitePosition.x < 1550 && _EnemyWhitePosition.x > 1480)
			{
				_EnemyVector[count_enemy].y += 1.8f;
			}
			else if (_EnemyWhitePosition.x < 1480)
				_EnemyVector[count_enemy].y -= 3.0f;

			for (int i = 0; i < 2; i++)
			{
				_EnemyWhite[count_enemy + i]->Next(1, 1);
			}
			
		}

		if (now - frame_start_white >= tick_per_frame)
		{
			frame_start_white = now;

			if (_EnemyWhitePosition.x > 1550)
			{
				for (int i = 0; i < 2; i++)
				{
					_EnemyWhite[count_enemy + i] ->Next();
				}
			}
		}

		//delete enemy 
		if (_EnemyWhitePosition.x < 1350) _EnemyWhitePosition.x = -100;
	}
	else if (ContraPositon.x > MARK2  && ContraPositon.x <MARK3 && mark == 2)
	{

		if (_EnemyWhitePosition.y != GROUNDY_ENEMY)
		{
			_EnemyWhitePosition.y = GROUNDY_ENEMY;
			_EnemyWhitePosition.x = ContraPositon.x + SCREEN_WIDTH;

			count_enemy++;
		}
			
		if (count_enemy < MAX_ENEMY_WHITE - 1  && _EnemyWhitePosition.x <= ContraPositon.x - SCREEN_WIDTH)
		{
			count_enemy += 3;
			_EnemyWhitePosition.x = ContraPositon.x + SCREEN_WIDTH;
		}

		if (now - frame_start_white >= tick_per_frame)
		{
			frame_start_white = now;

			for (int i = 0; i < 4; i++)
			{
				_EnemyWhite[count_enemy + i]->Next();
			}

		}
	}
	else if (ContraPositon.x > MARK3 && ContraPositon.x < MARK4 && mark == 3)
	{
		_EnemyWhitePosition.y = GROUNDY_ENEMY;

		if (count_enemy < MAX_ENEMY_WHITE - 1 && _EnemyWhitePosition.x <= ContraPositon.x - SCREEN_WIDTH / 2)
		{
			count_enemy += 3;
			_EnemyWhitePosition.x = 3000;
		}

		if (_EnemyWhitePosition.x < 2600)
		{

			if (_EnemyWhitePosition.x < 2600 && _EnemyWhitePosition.x > 2600-72 )
			{
				_EnemyVector[count_enemy].y += 1.0f;

			}
			else if (_EnemyWhitePosition.x < 2600 - 72)
				_EnemyVector[count_enemy].y -= 2.0f;

			for (int i = 0; i < 3; i++)
			{
				_EnemyWhite[count_enemy + i]->Next(1, 1);
			}
		}
		//if (_EnemyWhitePosition.x < 2624) _EnemyWhitePosition.x = -100;
	
		if (now - frame_start_white >= tick_per_frame)
		{
			frame_start_white = now;

			if (_EnemyWhitePosition.x > 2600)
			{
				for (int i = 0; i < 3; i++)
				{
					_EnemyWhite[count_enemy + i]->Next();
				}
			}
		}
	}
	else if (ContraPositon.x >MARK4 && ContraPositon.x < MARK5 && mark == 4)
	{
		/*if (_EnemyWhitePosition.x < MARK4  + 248) 
		_EnemyWhitePosition.x = -100;*/
		
		
		if (_EnemyWhitePosition.y != GROUNDY_ENEMY + 64)
		{
			_EnemyWhitePosition.y = GROUNDY_ENEMY + 64;
		//	count_enemy++;
		}

		if (_EnemyWhitePosition.x < MARK4 + 256) _EnemyWhitePosition.y = GROUNDY_ENEMY;

		if (count_enemy < MAX_ENEMY_WHITE - 1 && _EnemyWhitePosition.x <= ContraPositon.x - SCREEN_WIDTH / 2)
		{
			count_enemy++;
			_EnemyWhitePosition.x = MARK5 + SCREEN_WIDTH /4;
		}
		if (now - frame_start_white >= tick_per_frame)
		{
			frame_start_white = now;
			_EnemyWhite[count_enemy]->Next();
		}

	}
	
	/*_EnemyWhitePosition.x -= _EnemyVector[count_enemy].x;
	_EnemyWhitePosition.y += _EnemyVector[count_enemy].y;*/

	int vpx = ContraPositon.x - SCREEN_WIDTH / 2;
	if (vpx <= 0)
	{
		vpx = 0;
	}

	if (vpx >= GAMEWORLDWIDTH - SCREEN_WIDTH)
	{
		vpx = GAMEWORLDWIDTH - SCREEN_WIDTH;
	}
	
	_EnemyWhitePosition.x -= _EnemyVector[count_enemy].x;
	_EnemyWhitePosition.y += _EnemyVector[count_enemy].y;
	

	if (count_enemy > MAX_ENEMY_WHITE) count_enemy = MAX_ENEMY_WHITE;

}
void CEnemy_White::Render(D3DXVECTOR2 ContraPositon)
{

	int vpx_white = 0;
	if (vpx_white <= 0) vpx_white = 0;
	

	vpx_white = ContraPositon.x - SCREEN_WIDTH / 10;

	DWORD now = GetTickCount();

	Update(ContraPositon);

	//Enemy White
	if (ContraPositon.x  <  MARK1 && ContraPositon.x >100)
	{
		_EnemyWhite[count_enemy]->Render(_EnemyWhitePosition.x, _EnemyWhitePosition.y, vpx_white, VIEW_PORT_Y);
	}
	else if (ContraPositon.x > MARK1 && ContraPositon.x < MARK2 && mark == 1)
	{
		for (int i = 0; i < 2; i++)
		{
			_EnemyWhite[count_enemy + i]->Render(_EnemyWhitePosition.x + 13 * i, _EnemyWhitePosition.y, vpx_white, VIEW_PORT_Y);

		}
	}
	else if (ContraPositon.x > MARK2 && ContraPositon.x <MARK3 && mark == 2)
	{
		for (int i = 0; i < 4; i++)
		{
			_EnemyWhite[count_enemy + i]->Render(_EnemyWhitePosition.x + 180 * i, _EnemyWhitePosition.y, vpx_white, VIEW_PORT_Y);
		}
	}
	else if (ContraPositon.x >MARK3 && ContraPositon.x < MARK4 && mark == 3)
	{
		for (int i = 0; i < 3; i++)
		{
			_EnemyWhite[count_enemy + i]->Render(_EnemyWhitePosition.x + 13 * i, _EnemyWhitePosition.y, vpx_white, VIEW_PORT_Y);

		}
	}
	else if (ContraPositon.x >MARK4 && ContraPositon.x < MARK5 && mark == 4)
	{
		_EnemyWhite[count_enemy]->Render(_EnemyWhitePosition.x, _EnemyWhitePosition.y, vpx_white, VIEW_PORT_Y);
	}
}
CEnemy_White::~CEnemy_White()
{
}
